# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : delete_repeat_linknode.py
# @Time     : 2024/2/22 23:23
# @Author   : Robot-Zsj
"""
description:

逐个判断每个节点current的数据域与其下一个相邻节点的数据域是否相同。

如果相同，则current.next存储current.next.next节点的地址，也称为current节点指向current.next节点

如果不同，则该节点current向后移动，更新current为current.next所表示的节点


"""


class LinkNode:
    def __init__(self, val):
        self.val = val
        self.next = None

    def show(self):
        tmp = self
        res = []
        while tmp:
            if tmp.next:
                res.append(str(tmp.val))
                tmp = tmp.next
            else:
                res.append(str(tmp.val))
                tmp = None
        print('->'.join(res))


node1 = LinkNode(1)
node2 = LinkNode(2)
node3 = LinkNode(2)
node4 = LinkNode(3)
node5 = LinkNode(4)
node6 = LinkNode(3)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6

"""
排序链表
"""


def delete_repeat_sort_link_node(head):
    current = head
    while current and current.next:
        if current.next.val == current.val:
            current.next = current.next.next
        else:
            current = current.next
    return head


result = delete_repeat_sort_link_node(node1)
result.show()

"""
非排序链表
"""
node1 = LinkNode(1)
node2 = LinkNode(2)
node3 = LinkNode(2)
node4 = LinkNode(3)
node5 = LinkNode(4)
node6 = LinkNode(3)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6


def delete_repeat_link_node(head):
    if not head:
        return head
    lis = [head.val]
    pre = head
    current = pre.next
    while pre and pre.next:
        if current.val in lis:
            pre.next = current.next
            current = pre.next
        else:
            lis.append(current.val)
            pre = pre.next
            current = pre.next
    return head


res = delete_repeat_link_node(node1)
res.show()
